package io.github.maojian.bianrysearch;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: jian.mao
 * @Date: 2022-06-29 11:13
 * @Description: https://leetcode.cn/problems/check-if-n-and-its-double-exist/
 */
public class Q1346 {
    public boolean checkIfExist(int[] arr) {

        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
            } else {
                map.putIfAbsent(arr[i], 0);
            }
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                if (map.get(arr[i]) > 1) return true;
            } else {
                int doubleNum = arr[i] * 2;
                if (map.containsKey(doubleNum)) {
                    return true;
                }
            }
        }
        return false;
    }


    @Test
    public void test() {
        int[] arr = new int[]{-2, 0, 10, -19, 4, 6, -8};
        System.out.println(checkIfExist(arr));
    }
}
